package lu.med.praxiswahl.wahlumed.dao.beans;

import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.HollidayDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.HollidaysTBL;


/**
 * Session Bean implementation class HollidayDAO
 * @author Patrick kendzo
 * Email: pkendzo@msn.com
 */

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class HollidayDAO implements HollidayDAOInt
{

	@PersistenceContext(unitName = "gynaekomedic-ejbPU")
	private EntityManager	em	= null;
	
	
	/**
	 * Default constructor.
	 */
	public HollidayDAO()
	{

	}

	@Override
	public void delete(Long id)
	{
		HollidaysTBL holliday = em.find(HollidaysTBL.class, id);
		em.remove(holliday);
		em.flush();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<HollidaysTBL> listFeierTage()
	{
		try
		{
			return (List<HollidaysTBL>) em.createQuery(
					"select h from HollidaysTBL h where h.feiertag = true").getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("Database is empty", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<HollidaysTBL> listUrlaubTage()
	{
		try
		{
			return (List<HollidaysTBL>) em.createQuery(
					"select h from HollidaysTBL h where h.urlaub = true").getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("Database is empty", e);
		}
	}

	@Override
	public void persist(HollidaysTBL todo)
	{
		em.persist(todo);
		em.flush();
		
	}

	@Override
	public void update(HollidaysTBL todo)
	{
		em.merge(todo);
		em.flush();
		
	}

	@Override
	public HollidaysTBL getHollidayByID(Long id)
	{
		return null;
	}

}
